package com.example.robertluozizhao.framecollectdemo.adapter.base;

import android.annotation.SuppressLint;
import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * Created by robert.luozizhao on 2018/2/2.
 */

public abstract class BaseRcycleHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> viewArray;

    @SuppressLint("ResourceType")
    public BaseRcycleHolder(@IdRes int layoutId, ViewGroup parent) {
        super( LayoutInflater.from(parent.getContext()).inflate(layoutId,parent,false));
        viewArray = new SparseArray<>();
    }

    public BaseRcycleHolder(View itemView) {
        super(itemView);
        viewArray = new SparseArray<>();
    }

    public <T extends View> T getView(@IdRes int res) {
        View view = viewArray.get(res);
        if (view == null) {
            view = itemView.findViewById(res);
            viewArray.put(res, view);
        }
        return (T) view;
    }


}
